package com.zry.pda.utils

import android.text.TextUtils

class StringUtil {
	/**
	 * 获取String 如果值为空，则返回表示默认值
	 * @param value
	 * @param defaultValue
	 * @return
	 */
	fun getString(value: String, defaultValue: String): String {
		return if (TextUtils.isEmpty(value) || "null" == value) {
			defaultValue
		} else {
			value
		}
	}
	
	fun getCode(url: String): String {
		var str = url.replace(" ".toRegex(), "-")
		if (str.indexOf('-') > 0) {
			str = str.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]
		}
		if (str.indexOf("/") > 0) {
			val codes = str.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
			str = codes[codes.size - 2]
		}
		return str
	}
	
	/**
	 * 截取字符串，处理69位的网址
	 */
	fun getScanSaleNumber(productCode: String): String {
		return productCode.substring(productCode.length - 32, productCode.length - 15)
	}
	
	/**
	 * 截取字符串，处理87位的网址
	 */
	fun getScanSaleLongNumber(productCode: String): String {
		return productCode.substring(productCode.length - 17, productCode.length)
	}
	
	
	/**
	 * 截取字符串，处理69位的网址
	 */
	fun getSubstringCodeNumber(substringCode:String, startCode:Int, endCode:Int): String {
		return substringCode.substring(startCode, endCode)
	}
	
}
